const express = require("express")
const User = require('../mod/user')
const register = express.Router() //创建路由

const Mail = require("../tool/mail.js")
var {
    setCrypto
} = require('../tool/crypto.js')
const Rank = require("../mod/rank")


//找回密码
register.post("/getpassword", async (req, res) => {
    //需要参数 邮箱、验证码，两次密码，重设的username
    let {
        email,
        yzmval,
        password,
        checkPass,
        username
    } = req.body

    // console.log(email)
    //检查密码是否一致
    if (password !== checkPass) {
        return res.status(201).send({
            message: "密码不一致"
        })
    }


    //对密码加密
    password = setCrypto(password)

    //检查该邮箱是否被注册过
    let data = await User.findOne({
        email
    })
    if (!data) {
        return res.status(202).json({
            message: "该用户不存在，请重新检查你的邮箱是否正确"
        })
    }

    //检查验证码
    let obj = JSON.parse(process.env.code)
    let time1 = Date.now()
    if (yzmval != obj.code1) {
        return res.status(203).json({
            message: "验证码错误"
        })
    }

    if (time1 - obj.time >= 1000 * 60 * 5) {

        return res.status(204).json({
            message: "验证码已经失效,请重新获取"
        })

    }

    //验证都通过，开始注册
    let datalist = await User.update({
        email
    }, {
        email,
        username,
        password
    })

    let userData = await User.findOne({
        email
    })
    if (!userData) {
        return res.status(206).json({
            message: "请重试"
        })
    }
    let user_id = userData._id

    if (datalist) {
        //修改成功后，需要将所有与他username相关的重置
        //1.将上传的成绩 username 重置
        let data1 = await Rank.updateMany({
            user_id
        }, {
            username
        })
        if (data1) {
            return res.status(200).json({
                datalist,
                message: "ok"
            })
        }


    }

    res.status(205).json({
        message: "注册失败，请重试"
    })


})

//注册新用户
register.post("/", async (req, res) => {

    let {
        username,
        email,
        password,
        checkPass
    } = req.body
    if (password !== checkPass) {
        res.status(204).send({
            message: "密码不一致"
        })
        return
    }

    //具体使用，注意在登陆时也需要对传入的数据进行一次加密，再比较
    password = setCrypto(password)
    req.body.password = password
    let time1 = Date.now()
    let obj = JSON.parse(process.env.code)
    let olduser = await User.findOne({
        email
    })
    if (req.body.yzmval != obj.code1) {
        return res.status(201).send({
            message: "验证码错误"
        })
    }
    if (time1 - obj.time >= 1000 * 60 * 5) {

        return res.status(202).send({
            message: "验证码已经失效"
        })

    }
    if (olduser) {
        return res.status(203).send({
            message: "用户已经存在,注意邮箱的唯一性。如已注册，可以找回密码"
        })
    }
    let newuser = await new User(req.body).save()
    res.status(200).send({
        data: newuser,
        message: "注册成功"
    })
})



//发送邮箱验证码
register.get("/postmail", async (req, res) => {
    let {
        email
    } = req.query

    // let datalist = await User.findOne({
    //     email
    // })

    let codetime = parseInt(Math.random() * 10000)
    let data = await Mail.sendmail(email, codetime)
    let time = Date.now()

    if (data) {
        process.env.code = JSON.stringify({
            code1: codetime,
            time: time
        })
        return res.status(200).json({
            message: "发送成功"
        })
    }

    res.status(201).json({
        message: "发送err,请重新获取"
    })

})



module.exports = register